import { request } from '@/utils/request'

const base = '/roles'

export function findRoles (key) {
  return request({
    url: base,
    method: 'get',
    params: {
      key
    }
  })
}

export function addRole (role) {
  return request({
    url: base,
    method: 'post',
    data: role
  })
}

export function deleteRole (roleId) {
  return request({
    url: `${base}/${roleId}`,
    method: 'delete'
  })
}


export function findRoleCustomDataScopeDepts (roleId) {
  return request({
    url: `${base}/${roleId}/custom-data-scope/depts`,
    method: 'get'
  })
}

export function updateRole (data) {
  return request({
    url: `${base}/${data.id}`,
    method: 'patch',
    data
  })
}

/**
 * 保存功能权限
 * @param roleId
 * @param accessibleViews
 * @returns {AxiosPromise}
 */
export function assignPageAndButtonPermissions (roleId,accessibleViews) {
  return request({
    url: `${base}/${roleId}/assignment/pages-n-buttons`,
    method: 'patch',
    data: accessibleViews
  })
}

/**
 * 保存数据权限
 * @param roleId
 * @param roleDataScope
 * @returns {AxiosPromise}
 */
export function assignDataScopePermissions (roleId,roleDataScope) {
  return request({
    url: `${base}/${roleId}/assignment/data-scope`,
    method: 'patch',
    data: roleDataScope
  })
}

/**
 * 查询功能权限回显数据
 * @param roleId
 * @returns {AxiosPromise}
 */
export function findRoleAssignedPageAndButtonPermissions (roleId) {
  return request({
    url: `${base}/${roleId}/assigned/pages-n-buttons`,
    method: 'get'
  })
}

/**
 * 查询数据权限回显数据
 * @param roleId
 * @returns {AxiosPromise}
 */
export function findRoleAssignedDataScopePermissions (roleId) {
  return request({
    url: `${base}/${roleId}/assigned/data-scope`,
    method: 'get'
  })
}

// ------------

export function findRolePermissionsTree (roleCode) {
  return request({
    url: `${base}/${roleCode}/permissions/tree`,
    method: 'get'
  })
}

export function assignButtonPermissions (data) {
  return request({
    url: `${base}/assignment/pages/${data.page}/buttons`,
    method: 'patch',
    data
  })
}
